Date.prototype.Format = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(), 
        "h+": this.getHours(), 
        "m+": this.getMinutes(),
        "s+": this.getSeconds(), 
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[
            k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

$.format = function(source, params) {
    if (arguments.length == 1)
        return function () {
            var args = $.makeArray(arguments);
            args.unshift(source);
            return $.format.apply(this, args);
        };
    if (arguments.length > 2 && params.constructor != Array) {
        params = $.makeArray(arguments).slice(1);
    }
    if (params.constructor != Array) {
        params = [params];
    }
    $.each(params, function (i, n) {
        source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
    });
    return source;
};

$.sleep = (time) => {
    return new Promise((resolve) => setTimeout(resolve, time));
};

$.getGradientColor = (percentage, mode) => {
    var initR = 0, initG = 100 * mode, initB = 0, weight = 2 * mode;
    initR += weight * Math.min(50, percentage);
    initG -= weight * Math.max(0, percentage - 50);
    r = parseInt(initR);
    g = parseInt(initG);
    b = parseInt(initB);
    return "rgb(" + r + "," + g + "," + b + ")"; 
}

$.getKrd = (n, x0, y0, d, e) => {
    let krd = [];
    // even case
    if ((n & 1) == 0) {
        let a = d / 2;
        for (let i = 0; i < n; i++) {
            let x = a * (1 - Math.cos(2 * Math.PI * i / n));
            let y = -a * Math.sin(2 * Math.PI * i / n);
            krd.push([x0 + x, y0 + e * y]);
        }
    } else {
        let a = d / (2 * Math.cos(Math.PI / (2 * n)));
        for (let i = 0; i < n; i++) {
            let xk = a * (1 - Math.cos(2 * Math.PI * i / n));
            let yk = -a * Math.sin(2 * Math.PI * i / n);
            let x = yk * Math.sin(Math.PI / (2 * n)) + xk * Math.cos(Math.PI / (2 * n));
            let y = yk * Math.cos(Math.PI / (2 * n)) - xk * Math.sin(Math.PI / (2 * n));
            krd.push([x0 + x, y0 + e * y]);
        }
    }
    return krd;
}